﻿// -----------------------------------------------------------------------
// <copyright file="NodeWfDefaultData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db NodeWfDefault
    /// </summary>
    public class NodeWfDefaultData
    {
        /// <summary>
        /// Get NodeWfDefault By Id
        /// </summary>
        /// <param name="nodeWfDefaultId">Param nodeWfDefaultId</param>
        /// <returns>Return NodeWfDefault</returns>
        public NodeWfDefaultEN GetNodeWfDefaultById(int nodeWfDefaultId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                NodeWfDefaultEN nodeWfDefault = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeWfDefaultGetById, nodeWfDefaultId);
                while (reader.Read())
                {
                    nodeWfDefault = new NodeWfDefaultEN();

                    if (!reader.IsDBNull(0))
                    {
                        nodeWfDefault.NodeWfDefaultId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        nodeWfDefault.NodeWorkflowId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        nodeWfDefault.ProfileTypeId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        nodeWfDefault.DepartmentId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        nodeWfDefault.UserId = reader.GetInt32(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        nodeWfDefault.PositionsId = reader.GetInt32(5);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return nodeWfDefault;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all NodeWfDefault
        /// </summary>
        /// <returns>Return list all NodeWfDefault</returns>
        public IList<NodeWfDefaultEN> GetNodeWfDefaults()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<NodeWfDefaultEN> lstNodeWfDefault = new List<NodeWfDefaultEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeWfDefaultGetAll);
                while (reader.Read())
                {
                    NodeWfDefaultEN nodeWfDefault = new NodeWfDefaultEN();

                    if (!reader.IsDBNull(0))
                    {
                        nodeWfDefault.NodeWfDefaultId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        nodeWfDefault.NodeWorkflowId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        nodeWfDefault.ProfileTypeId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        nodeWfDefault.DepartmentId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        nodeWfDefault.UserId = reader.GetInt32(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        nodeWfDefault.PositionsId = reader.GetInt32(5);
                    }

                    lstNodeWfDefault.Add(nodeWfDefault);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstNodeWfDefault;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db NodeWfDefault
        /// </summary>
        /// <param name="nodeWfDefault">Paarm NodeWfDefault</param>
        /// <returns>Insert NodeWfDefault</returns>
        public int Insert(NodeWfDefaultEN nodeWfDefault)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.NodeWfDefault.NodeWorkflowId, nodeWfDefault.NodeWorkflowId),
                                    new SqlParameter(Constants.SqlParamerterName.NodeWfDefault.ProfileTypeId, nodeWfDefault.ProfileTypeId != 0 ? nodeWfDefault.ProfileTypeId : (object)DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.NodeWfDefault.DepartmentId, nodeWfDefault.DepartmentId),
                                    new SqlParameter(Constants.SqlParamerterName.NodeWfDefault.UserId, nodeWfDefault.UserId),
                                    new SqlParameter(Constants.SqlParamerterName.NodeWfDefault.PositionsId, nodeWfDefault.PositionsId)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeWfDefaultInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

         /// <summary>
        /// Insert to db NodeWfDefault
        /// </summary>
        /// <param name="nodeWfDefault">Paarm NodeWfDefault</param>
        /// <returns>Insert NodeWfDefault</returns>
        public int Insert_NodeWF(NodeWfDefaultEN nodeWfDefault)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.NodeWfDefault.NodeWorkflowId, nodeWfDefault.NodeWorkflowId),
                                    new SqlParameter(Constants.SqlParamerterName.NodeWfDefault.ProfileTypeId, nodeWfDefault.ProfileTypeId != 0 ? nodeWfDefault.ProfileTypeId : (object)DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.NodeWfDefault.DepartmentId, nodeWfDefault.DepartmentId),
                                    new SqlParameter(Constants.SqlParamerterName.NodeWfDefault.UserId, nodeWfDefault.UserId),
                                    new SqlParameter(Constants.SqlParamerterName.NodeWfDefault.PositionsId, nodeWfDefault.PositionsId)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeWfDefaultInsertWF, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

         /// <summary>
        /// Update to db NodeWfDefault
        /// </summary>
        /// <param name="nodeWfDefault">Param NodeWfDefault</param>
        public void Update(NodeWfDefaultEN nodeWfDefault)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.NodeWfDefault.NodeWfDefaultId, nodeWfDefault.NodeWfDefaultId),
                                    new SqlParameter(Constants.SqlParamerterName.NodeWfDefault.NodeWorkflowId, nodeWfDefault.NodeWorkflowId),
                                    new SqlParameter(Constants.SqlParamerterName.NodeWfDefault.ProfileTypeId, nodeWfDefault.ProfileTypeId),
                                    new SqlParameter(Constants.SqlParamerterName.NodeWfDefault.DepartmentId, nodeWfDefault.DepartmentId),
                                    new SqlParameter(Constants.SqlParamerterName.NodeWfDefault.UserId, nodeWfDefault.UserId),
                                    new SqlParameter(Constants.SqlParamerterName.NodeWfDefault.PositionsId, nodeWfDefault.PositionsId)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeWfDefaultUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete NodeWfDefault by nodeWfDefaultId
        /// </summary>
        /// <param name="nodeWfDefaultId">param nodeWfDefaultId</param>
        public void Delete(int nodeWfDefaultId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.NodeWfDefault.NodeWfDefaultId, nodeWfDefaultId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeWfDefaultDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get List NodeWfDefault By NodeWorkflowId
        /// </summary>
        /// <param name="nodeWorkflowId">Param nodeWorkflowId</param>
        /// <returns>Return List NodeWfDefault</returns>
        public IList<NodeWfDefaultEN> GetNodeWfDefaultByNodeWorkflowId(int nodeWorkflowId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<NodeWfDefaultEN> lstNodeWfDefault = new List<NodeWfDefaultEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeWfDefaultGetByNodeWorkflowId, nodeWorkflowId);
                while (reader.Read())
                {
                    NodeWfDefaultEN nodeWfDefault = new NodeWfDefaultEN();

                    if (!reader.IsDBNull(0))
                    {
                        nodeWfDefault.NodeWfDefaultId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        nodeWfDefault.NodeWorkflowId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        nodeWfDefault.ProfileTypeId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        nodeWfDefault.DepartmentId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        nodeWfDefault.UserId = reader.GetInt32(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        nodeWfDefault.PositionsId = reader.GetInt32(5);
                    }

                    lstNodeWfDefault.Add(nodeWfDefault);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstNodeWfDefault;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get List NodeWfDefault By nodeWorkflowId
        /// </summary>
        /// <param name="nodeWorkflowId">param nodeWorkflowId</param>
        /// <param name="profileTypeId">param ProfileTypeId</param>
        /// <param name="departmentId">param DepartmentId</param>
        /// <param name="userId">param UserId</param>
        /// <param name="postionId">param PostionId</param>
        /// <returns>Return NodeWfDefaultEN</returns>
        public NodeWfDefaultEN GetNodeWfDefaultBy_DepartmentAndProfileTypeId(int nodeWorkflowId, int profileTypeId, int departmentId, int userId, int postionId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                NodeWfDefaultEN nodeWfDefault = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeWfDefaultGetByDepartmentAndProfileTypeId, nodeWorkflowId, profileTypeId, departmentId, userId, postionId);
                while (reader.Read())
                {
                    nodeWfDefault = new NodeWfDefaultEN();

                    if (!reader.IsDBNull(0))
                    {
                        nodeWfDefault.NodeWfDefaultId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        nodeWfDefault.NodeWorkflowId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        nodeWfDefault.ProfileTypeId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        nodeWfDefault.DepartmentId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        nodeWfDefault.UserId = reader.GetInt32(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        nodeWfDefault.PositionsId = reader.GetInt32(5);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return nodeWfDefault;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
         }

        /// <summary>
        /// Gets the node wf default by node workflow_ department_ user.
        /// </summary>
        /// <param name="nodeWorkflowId">The node workflow id.</param>
        /// <param name="departmentId">The department id.</param>
        /// <param name="userId">The user id.</param>
        /// <param name="profileTypeId">The profileTypeId.</param>
        /// <returns>return NodeWfDefaultEN</returns>
        public NodeWfDefaultEN GetNodeWfDefaultByNodeWorkflow_Department_User_ProfileType(int nodeWorkflowId, int departmentId, int userId, int profileTypeId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                NodeWfDefaultEN nodeWfDefault = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeWfDefaultGetByNodeWorkflowDepartmentUserProfileType, nodeWorkflowId, departmentId, userId, profileTypeId);
                while (reader.Read())
                {
                    nodeWfDefault = new NodeWfDefaultEN();

                    if (!reader.IsDBNull(0))
                    {
                        nodeWfDefault.NodeWfDefaultId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        nodeWfDefault.NodeWorkflowId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        nodeWfDefault.ProfileTypeId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        nodeWfDefault.DepartmentId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        nodeWfDefault.UserId = reader.GetInt32(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        nodeWfDefault.PositionsId = reader.GetInt32(5);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return nodeWfDefault;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}